/**
 * Copyright (c) 2013-2016, Jieven. All rights reserved.
 *
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
 * To use it on other terms please contact us at 1623736450@qq.com
 */
package com.rock.sys.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eova.common.Easy;
import com.eova.common.base.BaseCache;
import com.eova.common.utils.EncryptUtil;
import com.eova.common.utils.xx;
import com.eova.config.EovaConst;
import com.eova.core.meta.MetaUtil;
import com.eova.engine.DynamicParse;
import com.eova.engine.EovaExp;
import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.model.Role;
import com.eova.model.RoleObjField;
import com.eova.model.User;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rock.sys.config.RockConst;

/**
 * 元数据相关获取
 *
 * @author jadison
 *
 */
public class RockUserController extends Controller {
	
	/**
	 * 增加APP用户对应的系统用户
	 */
	public void addAppSysUser() {

        //设置系统用户的角色，便于通过元数据统一控制数据访问权限
        Role role = Role.dao.findFirst("select * from eova_role where name=?", RockConst.TENANT_APP_USER_ROLE);
        
		User lUser = new User();
		lUser.set("id", this.getPara("id"));
		lUser.set("login_id", this.getPara("login_id"));
		lUser.set("login_pwd", EncryptUtil.getSM32(this.getPara("login_pwd")));
		lUser.set("rid", role.get("id"));
		lUser.set("rids", role.get("id"));
		lUser.save();
		
		renderJson(new Easy("新增用户成功",true));
	}
	
	/**
	 * 增加平台租户管理员对应的系统用户
	 */
	public void addTenantAdminSysUser() {

        //设置系统用户的角色，便于通过元数据统一控制数据访问权限
        Role role = Role.dao.findFirst("select * from eova_role where name=?", RockConst.TENANT_ADMIN_ROLE);
        
        if(role == null) {
        	renderJson(new Easy("新增用户失败，未找到租户管理员专属角色",false));
        	return;
        }
        
		User lUser = new User();
		lUser.set("id", this.getPara("id"));
		lUser.set("login_id", this.getPara("login_id"));
		lUser.set("login_pwd", EncryptUtil.getSM32(this.getPara("login_pwd")));
		lUser.set("rid", role.get("id"));
		lUser.set("rids", role.get("id"));
		lUser.save();
		
		//将角色id返回
		Map<String,String> res = new HashMap<String,String>();
		res.put("rid", role.get("id"));
		
		renderJson(JsonKit.toJson(res));
	}
}